﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using ExpertMIS.Entities;
using SYS.Common.Broker;
using IBatisNet.Extend;
using IBatisNet.Common.Utilities;

namespace ExpertMIS.Admin
{
    public partial class SelectUserList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void gvList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow) 
            {
                DataRowView row = e.Row.DataItem as DataRowView;
                CheckBox chk = e.Row.Cells[0].FindControl("cbChecked") as CheckBox;
                chk.Attributes.Add("class", "gridview_row_checkbox");
                chk.Attributes.Add("rowid", row["F_OID"].ToString());
                chk.Attributes.Add("rowname", row["F_Name"].ToString());
            }
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            var filterString = "";
            if (!string.IsNullOrEmpty(txUserName.Text) && txUserName.Text.Trim().Length > 0) 
            {
                filterString = string.Format(" F_Name like '%{0}%' ", txUserName.Text);
            }

            if (ddlIsExpert.SelectedValue != "all")
            {
                 if (filterString.Length > 0)
                 {
                     filterString += " AND ";
                 }

                 if (ddlIsExpert.SelectedValue.ToLower() == "true")
                 {

                     filterString += " F_IsExpert = 1 ";
                 }
                 else
                 {
                     filterString += " F_IsExpert = 0 ";
                 }
            }

            QueryResult(filterString);
        }

        private void QueryResult(string searchKey)
        {
            string ObjectName = "ExpertMIS.UserAuditListView";
            SYS.Web.Utilities.ColumnGenerator.Generate(gvList, ObjectName, true);

            ObjectBroker brokerInfo = ObjectManagerBroker.GetObjectManagerBroker().GetObjectBrokerByObjectName(ObjectName);
            BaseBroker broker = null;
            if (brokerInfo != null) // 外连了一个Broker对象
            {
                broker = brokerInfo.CreateInstance() as BaseBroker;
            }

            PagingSize pagingSize = new PagingSize();

            pagingSize.Limit = gvList.PageSize;
            pagingSize.Start = (gvList.PageIndex -1)*gvList.PageSize;
            pagingSize.SearchKey = broker != null ? broker.ProcessQueryCondition(QueryExpression.CreateQueryExpression(searchKey)) : searchKey;
            pagingSize.SortExpression = string.IsNullOrEmpty(gvList.SortExpression) ? " F_OID " + "  DESC" : gvList.SortExpression;
            gvList.DataSource = SQLBuilderBroker.GetSQLBuilderBroker().GetObjectView(ObjectName, pagingSize);
            gvList.AutoGenerateColumns = false;
            gvList.DataBind();  
        }

        private void CheckRows(bool bChecked)
        {
            // Iterate through the Rows collection of the GridView
            // control and show or hide the rows based on the value
            // of the show parameter.
            foreach (GridViewRow row in gvList.Rows)
            {
                CheckBox chk = row.Cells[0].FindControl("cbChecked") as CheckBox;
                chk.Checked = bChecked;
            }
        }

        protected void cbSelectedAll_CheckedChanged(object sender, EventArgs e)
        {
            // Get the CheckBox control that indicates whether to show or 
            // hide the rows in the GridView control. The sender parameter
            // contains the control that raised the event.
            CheckBox showCheckBox = (CheckBox)sender;

            // Show or hide the rows of the GridView control based
            // on the check box value selected by the user.
            if (showCheckBox.Checked)
            {
                CheckRows(true);
            }
            else
            {
                CheckRows(false);
            }
        }
    }
}
